package com.action;

import java.io.File;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.config.PrimaryGenerater;
import com.config.RedisConfig;
import com.dto.Apply;
import com.service.ApplyService;


@RestController
public class ApplyAction {
	@Autowired
	private ApplyService applyService;
	@Autowired
	private RedisConfig redisConfig;
	@Autowired
	private PrimaryGenerater primaryGenerater;
	
	public ModelAndView addApplyInfo(HttpServletRequest request,Apply apply,@RequestParam(value="img")MultipartFile[] img) {
		ModelAndView mv =new ModelAndView();
		String pay_num = redisConfig.getUpValue();
//		String pay_num = primaryGenerater.toDateString();
		apply.setPay_num(pay_num);
		apply.setS_id(pay_num);
		com.dto.Imgtab myImg = new com.dto.Imgtab();
		myImg.setId(pay_num);
		for (int i = 0; i < img.length; i++) {
			MultipartFile picture = img[i];
			if(picture.getSize()>0) {
				String oldName = picture.getOriginalFilename();
				String last = oldName.substring(oldName.lastIndexOf("."));
				if(".jpg".equals(last) ||".png".equals(last) ||".jpeg".equals(last) ||".gif".equals(last) ||".bmp".equals(last)) {
					UUID uuid = UUID.randomUUID();
					String newName = uuid.toString()+last;
					File file = new File("\\\192.168.1.115\\static\\img\\"+newName);
					if(!file.exists()) {
						file.getParentFile().mkdirs();
					}
					try {
						picture.transferTo(file);
					} catch (Exception e) {
						e.printStackTrace();
					}
					//上传身份证正反面照片
					if(i==0) {
						myImg.setName(newName);
					}
					if(i==1) {
						myImg.setName(newName);
					}
					
				}
			}
		}
		int a = applyService.addApplyInfo(apply);
		if(a>0) {
			mv.setViewName("/.jsp");
		}
		return mv;
		
	}
}
